//
//  SwitchView.m
//  Molian
//
//  Created by 胡津 on 15/3/12.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "SwitchView.h"

@implementation SwitchView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)initview
{
    self.backgroundColor = [UIColor clearColor];
    //未被选择时候btn颜色
    black = [UIColor grayColor];
    //选中时
    light = [UIColor whiteColor];
    
    normalFont = [UIFont fontWithName:FontName size:16];
    lightFont = [UIFont fontWithName:FontName size:16];
    
    for (int i = 0; i < _TitleArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*self.frame.size.width/self.TitleArr.count, 0, self.frame.size.width/self.TitleArr.count, titleHeight);
        [btn setTitle:_TitleArr[i] forState:UIControlStateNormal];
        btn.titleLabel.font = normalFont;
        [btn setTitleColor:black forState:UIControlStateNormal];
        btn.tag=101+i;
        [btn addTarget:self action:@selector(btAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            [btn setTitleColor:light forState:UIControlStateNormal];
            btn.titleLabel.font = lightFont;
        }
        [self addSubview:btn];
        
    }
    
    
    //默认情况
    currentTag=101;
    
    //滑动线
    Line = [[UIView alloc]initWithFrame:CGRectMake((self.frame.size.width/self.TitleArr.count-60)/2, 31, 60, 3)];
    Line.backgroundColor = APPCOLOR(1);
    Line.layer.masksToBounds = YES;//开启圆角
    [Line.layer setCornerRadius:1.5];
    [self addSubview:Line];
    
    //分割线
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width/2, 5, 0.5, 20)];
    line.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6];
    [self addSubview:line];


}

//页面滑动顺便移动滑动线
-(void)sliding:(CGFloat)x
{
    CGFloat w = self.frame.size.width/self.TitleArr.count;
    w = (w-60)/2.0;
    Line.frame = CGRectMake(w + x/DeviceWidth * (self.frame.size.width/self.TitleArr.count), 31, 60, 3);
   // Line.frame =CGRectMake(((DeviceWidth-200)/2)+(x/(DeviceWidth/100))+20, 31, 60, 3);
}

//按下按钮
-(void)swipeAction:(NSInteger)tag{
    
    UIButton *btn1 = (UIButton*)[self viewWithTag:101];
    UIButton *btn2 = (UIButton*)[self viewWithTag:102];
    if ([self.delegate respondsToSelector:@selector(clickButtonIsLeft:)]) {
        [self.delegate clickButtonIsLeft:(tag ==101)?YES:NO];
    }
    switch (tag) {
        case 101:
            currentTag=101;
            [btn1 setTitleColor:light forState:UIControlStateNormal];
            [btn2 setTitleColor:black forState:UIControlStateNormal];
            
            btn1.titleLabel.font = lightFont;
            btn2.titleLabel.font = normalFont;
            break;
        case 102:
            
            currentTag=102;
            [btn1 setTitleColor:black forState:UIControlStateNormal];
            [btn2 setTitleColor:light forState:UIControlStateNormal];
            
            btn1.titleLabel.font = normalFont;
            btn2.titleLabel.font = lightFont;
            
            
            break;

        default:
            
            break;
    }
    if (_ButtonActionBlock) {
        _ButtonActionBlock(currentTag);
    }
    
}


-(void)btAction:(UIButton *)button{
    [self swipeAction:button.tag];
}
- (void)dealloc
{
    self.delegate = nil;
}
@end
